﻿/*
Copyright (c) 2010, hiSoft
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the hiSoft nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE HISOFT AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL HISOFT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Threading;
using System.IO;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Orchard.Test.Library;
using Selenium;

namespace Orchard.Test.WorkThoughCases.Auto
{
    [TestClass]
    public class DoActionToOrchardExe
    {
        private TestLibrary TestLibrary;
        private ISelenium selenium;

        [TestInitialize]
        public void SetupTest()
        {
            TestLibrary = TestLibrary.SetupTest(this.ToString());
            selenium = TestLibrary.Selenium;
        }

        [TestCleanup]
        public void TeardownTest()
        {
            TestLibrary.ShutDown();
        }

        [TestMethod]
        public void WorkThoughCases_DoActionToOrchardExe()
        {
            string errorMsg="";
            string BlogVersion="1.2.0";
            string helpCommand = "help commands";
            string output = TestLibrary.OrchardExeHelper.ExecuteCommand(helpCommand);
            Assert.IsTrue(output.Contains("List of available commands"));

            string availabelCommand = "cultures list";
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(String.Format("help {0}", availabelCommand));
            Assert.IsTrue(output.Contains("List site cultures"));

            availabelCommand = "user create";
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(String.Format("help {0}", availabelCommand));
            Assert.IsTrue(output.Contains("Creates a new User"));

            availabelCommand = "cultures list";
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Listing Cultures:"));

            availabelCommand = "cultures get site culture";
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Site Culture is en-US"));

            availabelCommand = "cultures set site culture af-ZA";
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Site culture set to af-ZA successfully"));

            string userName = String.Format("User{0}", UIHelperBase.GenerateUniqueIdentifier(DateTime.Now));
            string email = String.Format("{0}@microsoft.com", userName);
            string passWord = TestLibrary.Consts.CommonPassword;

            availabelCommand = String.Format("user create /UserName:{0} /Password:{1} /Email:{2}", userName, passWord, email);

            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("User created successfully"));

            availabelCommand = String.Format("menuitem create /MenuPosition:1 /MenuText:Menu01ByCmd /Url:www.bing.com /OnMainMenu:true");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Menu item created successfully"));

            try
            {
                availabelCommand = String.Format("theme list");
                output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
                Assert.IsTrue(output.Contains("Current theme"));
                Assert.IsTrue(output.Contains("List of available themes"));

                availabelCommand = String.Format("theme list /Summary:true");
                output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
                Assert.IsTrue(output.Contains("The Theme Machine"));
            }
            catch (Exception ex)
            { errorMsg += ex.ToString(); }

            TestLibrary.UserHelper.LogOnAsAdmin();
            TestLibrary.ThemeHelper.InstallTestThemeByNameInGallery("Contoso");
            Thread.Sleep(3000);

            availabelCommand = String.Format("theme activate Contoso");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Theme \"Contoso\" activated"));

            availabelCommand = String.Format("feature list");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Category:") && output.Contains("Name:") && output.Contains("State:") && output.Contains("Module:"));

            availabelCommand = String.Format("feature list /Summary:true");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Common") && output.Contains("Enabled") && output.Contains("Disabled"));

            availabelCommand = String.Format("feature enable Orchard.Email");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Email Messaging was enabled"));

            availabelCommand = String.Format("feature disable Orchard.Email");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Disabled features  Orchard.Email"));

            availabelCommand = String.Format("package create Orchard.Blogs C:\\User");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains(string.Format("Package \"C:\\User\\Orchard.Module.Orchard.Blogs.{0}.nupkg\" successfully created", BlogVersion)));

            string moduleName = "Lucene";
            string packageSrcPath = TestLibrary.Consts.OrchardTestDataPath + "\\ModuleResource";
            string packageDesPath = TestLibrary.Consts.OrchardSrcPath + "\\packages";
            string rightVersion = "1.2.0";

            if (Directory.Exists(String.Format(packageDesPath + "\\Orchard.Module.{0}.{1}", moduleName, rightVersion)))
            {
                Directory.Delete(String.Format(packageDesPath + "\\Orchard.Module.{0}.{1}", moduleName, rightVersion), true);
            }
            TestLibrary.OrchardExeHelper.ExecuteCommand("feature enable Gallery");
            try
            {
                output = TestLibrary.OrchardExeHelper.ExecuteCommand(String.Format("package install Orchard.Module.{0} {1} /version:{2}", moduleName, packageSrcPath, rightVersion));
                Assert.IsTrue(output.Contains(String.Format("Successfully installed 'Orchard.Module.{0} {1}'", moduleName, rightVersion)));
                output = TestLibrary.OrchardExeHelper.ExecuteCommand(String.Format("package uninstall Orchard.Module.{0}", moduleName));
                Assert.IsTrue(output.Contains(String.Format("Successfully uninstalled 'Orchard.Module.{0} {1}'", moduleName, rightVersion)));
            }
            catch (Exception exx) { errorMsg += exx.ToString(); }
            string blogTitle = String.Format("blog{0}", UIHelperBase.GenerateUniqueIdentifier(DateTime.Now));
            availabelCommand = String.Format("blog create /Slug:{0} /Title:{0} /MenuText:{0} /Owner:{1}", blogTitle, TestLibrary.Consts.AdminUserName);
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Blog created successfully"));

            string existBlogSlug = TestLibrary.DefaultBlog.Permalink;
            string nonExistBlogSlug = UIHelperBase.GenerateUniqueIdentifier(DateTime.Now);
            string feedUrl = "http://orchard.codeplex.com/Project/ProjectRss.aspx";
            availabelCommand = String.Format("blog import /Slug:{0} /Owner:{1} /FeedUrl:{2}", existBlogSlug, TestLibrary.Consts.AdminUserName, feedUrl);
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Import feed completed"));

            availabelCommand = String.Format("feature enable Orchard.MultiTenancy");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("MultiTenancy was enabled"));

            availabelCommand = String.Format("tenant list");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Name:") && output.Contains("State:"));

            availabelCommand = String.Format("tenant add 123");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Creating tenant"));

            availabelCommand = String.Format("tenant info 123");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Name:") && output.Contains("State:"));

            availabelCommand = String.Format("feature enable Orchard.Search Lucene");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Search was enabled") && output.Contains("Indexing was enabled") && output.Contains("Lucene was enabled"));

            availabelCommand = String.Format("index rebuild");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Index is now being rebuilt"));

            availabelCommand = String.Format("index update");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Index is now being updated"));

            availabelCommand = String.Format("index stats");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Number of indexed documents:"));

            availabelCommand = String.Format("index search /query:content");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("results") && output.Contains("End of search results"));

            availabelCommand = String.Format("index refresh /contentitem:31");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Content Item marked for reindexing"));

            availabelCommand = String.Format("index delete /contentitem:32");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Content Item marked for deletion"));

            availabelCommand = String.Format("layer create layer1 /LayerRule:true /Description:123/Owner:admin");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Layer created successfully"));

            availabelCommand = String.Format("widget create HtmlWidget /Title:widget1 /Zone:Header /Position:2 /Layer:Default /Identity:123 /Owner:admin /Text:123 /UseLoremIpsumText:true");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Widget created successfully"));

            availabelCommand = String.Format("feature enable Orchard.CodeGeneration");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Code Generation was enabled"));

            if (Directory.Exists(String.Format("{0}\\module1", TestLibrary.Consts.OrchardModulesPath)))
            {
                try
                {
                    Directory.Delete(String.Format("{0}\\module1", TestLibrary.Consts.OrchardModulesPath), true);
                }
                catch (Exception)
                { }
            }
            availabelCommand = String.Format("codegen module module1");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Module module1 created successfully"));

            availabelCommand = String.Format("codegen datamigration module1");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Data migration created successfully in Module module1"));

            availabelCommand = String.Format("codegen controller module1 controller1");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Controller controller1 created successfully in Module module1"));
            if (Directory.Exists(String.Format("{0}\\module1", TestLibrary.Consts.OrchardModulesPath)))
            {
                try
                {
                    Directory.Delete(String.Format("{0}\\module1", TestLibrary.Consts.OrchardModulesPath), true);
                }
                catch (Exception)
                { }
            }

            if (Directory.Exists(String.Format("{0}\\theme1", TestLibrary.Consts.OrchardThemesPath)))
            {
                try
                {
                    Directory.Delete(String.Format("{0}\\theme1", TestLibrary.Consts.OrchardThemesPath), true);
                }
                catch (Exception)
                { }
            }
            availabelCommand = String.Format("codegen theme theme1");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Theme theme1 created successfully"));

            if (Directory.Exists(String.Format("{0}\\theme1", TestLibrary.Consts.OrchardThemesPath)))
            {
                try
                {
                    Directory.Delete(String.Format("{0}\\theme1", TestLibrary.Consts.OrchardThemesPath), true);
                }
                catch (Exception)
                { }
            }

            try
            {
                availabelCommand = String.Format("feature enable Profiling");
                output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
                Assert.IsTrue(output.Contains("Profiling was enabled") && output.Contains("Experimental was enabled"));

                availabelCommand = String.Format("add profiling data");
                output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
                Assert.IsTrue(output.Contains("Finished adding profiling data")); // the former output info :"AddProfilingData completed"

                availabelCommand = String.Format("add users");
                output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
                Assert.IsTrue(output.Contains("Success"));
            }
            catch (Exception ex)
            {
                errorMsg += ex.ToString();
            }

            availabelCommand = String.Format("feature enable DatabaseUpdate");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Database Update was enabled"));

            availabelCommand = String.Format("update database");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Database updated"));

            availabelCommand = String.Format("create tables Orchard.Blogs /drop:false");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Tables created") && output.Contains("Creating table"));

            availabelCommand = String.Format("create tables Orchard.Blogs /drop:true");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Tables created") && output.Contains("Creating table") && output.Contains("Dropping table"));

            availabelCommand = String.Format("feature enable Orchard.Migrations");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Migrations was enabled"));

            availabelCommand = String.Format("upgrade database Orchard.Blogs");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Database upgraded"));

            availabelCommand = String.Format("page create /Slug:123 /Title:123 /Path:123 /Text:123 /Owner:admin /Homepage:true /Publish:true /UseWelcomeText:true");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Page created successfully"));

            availabelCommand = String.Format("recipes harvest Orchard.Blogs");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("List of available recipes"));

            availabelCommand = String.Format("feature enable Orchard.Warmup");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Warmup was enabled"));

            availabelCommand = String.Format("warmup generate /Force:true");
            output = TestLibrary.OrchardExeHelper.ExecuteCommand(availabelCommand);
            Assert.IsTrue(output.Contains("Generation finished"));
            if (errorMsg.Length > 2)
            {
                Assert.IsTrue(false, "Bug? Please check test case code. "+errorMsg);
            }
        }

    }
}
